<!DOCTYPE html>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>用户编辑</title>
    {% include 'admin/common/header.html' %}
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">

                <div class="layui-form-item layui-hide">
                    <label class="layui-form-label">编号</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ goods.id }}" name="id" lay-verify="title"
                               autocomplete="off" placeholder="请输入标题" class="layui-input">
                    </div>
                </div>

               <div class="layui-form-item">
                    <label class="layui-form-label">商品名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="gname" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                               class="layui-input" value="{{ goods.gname  }}">
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">价格</label>
                    <div class="layui-input-block">
                        <input type="number" name="price" lay-verify="pass" autocomplete="off" placeholder="请输入标题"
                               class="layui-input" value="{{ goods.price }}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">

                        <select name="gtypes" lay-verify="title">
                            <option value="{{ classify.classifyid }}">{{ classify.gname}}</option>
                            {% for cla in classifys %}
                                <option value="{{ cla.classifyid }}">{{ cla.gname}}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">库存</label>
                    <div class="layui-input-block">
                        <input type="number" name="gnum" lay-verify="title" autocomplete="off" placeholder="请输入标题"
                               class="layui-input" value="{{ goods.gnum }}">
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">新品</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="isnew" lay-skin="switch"
                               lay-text="新品|普通"/>
                    </div>
                </div>


                <div class="layui-form-item">
                    <label class="layui-form-label">热卖</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="ishot" lay-skin="switch"
                               lay-text="热卖|普通"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">是否上架</label>
                    <div class="layui-input-block">
                        <input type="checkbox" name="is_on_sale" lay-skin="switch"
                               lay-text="暂不上架|上架"/>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">产品介绍</label>
                    <div class="layui-input-block">
                        <textarea name="desc" placeholder="请输入内容" class="layui-textarea">{{ goods.content }}</textarea>
                    </div>
                </div>

                <fieldset class="layui-elem-field layui-field-title" style="margin: -8px 0px 10px 10px;">
                    <legend>商品图片</legend>
                </fieldset>
                <div class="layui-upload-drag" id="uptest"
                     style="width: 70px; display: inline-block;margin-left: 40px;">
                    <i class="layui-icon"></i>
                    <p>点击上传，或将文件拖拽到此处</p>
                </div>
                <div id="uploadDemoView">
                    <img src='{{ goods.gphoto }}' alt='上传成功后渲染' style='max-width: 103px;position: relative;margin-left: 190px;margin-top: -143px;display: inline-block'>
                </div>

                {#                <div class="layui-form-item">#}
                {#                    <label class="layui-form-label">部门</label>#}
                {#                    <div class="layui-input-block">#}
                {#                        <ul id="selectParent" name="deptId" class="dtree" data-id="0"></ul>#}
                {#                    </div>#}
                {#                </div>#}


                {#                <div class="layui-form-item">#}
                {#                    <label class="layui-form-label">角色</label>#}
                {#                    <div class="layui-input-block">#}
                {#                        {% for role in roles %}#}
                {#                            <input{% if role.id in checked_roles %}#}
                {#                                checked#}
                {#                            {% endif %}#}
                {#                                value="{{ role.id }}" title="{{ role.name }}" type="checkbox"#}
                {#                                name="roleIds" lay-skin="primary">#}
                {#                        {% endfor %}#}
                {#                    </div>#}
                {#                </div>#}


            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-update">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'admin/common/footer.html' %}
<script>
    layui.use(['upload', 'form', 'jquery', 'dtree'], function () {
        let form = layui.form
        let $ = layui.jquery
        let dtree = layui.dtree
        let upload = layui.upload
        let imgpath='{{ goods.gphoto }}'
        {#dtree.renderSelect({#}
        {#    elem: '#selectParent',#}
        {#    url: '/dept/tree',#}
        {#    method: 'get',#}
        {#    selectInputName: {nodeId: 'deptId', context: 'deptName'},#}
        {#    skin: 'layui',#}
        {#    dataFormat: 'list',#}
        {#    response: {treeId: 'deptId', parentId: 'parentId', title: 'deptName'},#}
        {#    selectInitVal: "{{ user.dept_id }}"#}
        {#//})#}

        //拖拽上传
        upload.render({
            elem: '#uptest'
            , url: '{{ url_for('goods.upphoto') }}' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
            {#,url: 'https://httpbin.org/post'#}
            , exts: 'jpg|png|gif|bmp|jpeg'
            , done: function (res) {
                layer.msg('上传成功', {icon: 1});
                {#console.log(res.img)#}
                layui.$('#uploadDemoView').find('img').attr('src', res.data.src);
                imgpath = res.data.src
                console.log(imgpath)
                {#layui.$('#uploadDemoView').removeClass('layui-hide').find('img').attr('src', res.files.file);#}
                console.log(res)
            }
        });

        form.on('submit(user-update)', function (data) {
            {#let roleIds = ''#}
            {#$('input[type=checkbox]:checked').each(function () {#}
            {#    roleIds += $(this).val() + ','#}
            {#//})#}
            {#roleIds = roleIds.substr(0, roleIds.length - 1)#}
            {#data.field.file = roleIds#}
            data.field.file=imgpath
            console.log(data.field)


            $.ajax({
                url: '/goods/update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
                            parent.layui.table.reload('user-table')
                        })
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000})
                    }
                }
            })
            return false
        })
    })
</script>
</body>
</html>